package com.dabai.dbutils.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.dabai.dbutils.ui.DebugMessages;
import com.dabai.dbutils.ui.ErrorMessage;

import java.util.ArrayList;
import java.util.Collections;

/**
 * 用于输出debug的工具类
 *
 * @author BAI
 */
public class DebugUtils {

    Intent intent;
    Context context;

    public DebugUtils(Context context){
        this.context = context;
        intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    }

    /**
     * 显示调试信息
     *
     * @param text 文本
     */
    public void showDebugMessage(Object text){
        intent.setClass(context, DebugMessages.class);
        intent.putExtra("message","调试来源:"+context.getClass()+"\n\n"+text.toString());
        context.startActivity(intent);
    }

    /**
     * 显示错误消息
     *
     * @param text 文本
     */
    public void showErrorMessage(Object text){
        intent.setClass(context, ErrorMessage.class);
        intent.putExtra("message","错误来源:"+context.getClass()+"\n\n"+text.toString());
        context.startActivity(intent);
    }


    /**
     * 活动的名字
     * 返回所有 activity名  用于调试
     *
     * @param c c
     * @return {@link ArrayList<String>}
     */
    public static ArrayList<String> getActivitiesName(Context c) {

        ArrayList<String> arrayList = new ArrayList<>();

        PackageManager packageManager = c.getPackageManager();
        PackageInfo packageInfo;
        try {
            packageInfo = packageManager.getPackageInfo(
                    c.getPackageName(), PackageManager.GET_ACTIVITIES);

            for (ActivityInfo activity : packageInfo.activities) {
                Class<?> aClass = Class.forName(activity.name);
                arrayList.add(aClass + "\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Collections.reverse(arrayList);
        return arrayList;

    }
}
